package com.py.credit.controller;

import com.alibaba.fastjson.JSONArray;
import com.py.credit.common.Page;
import com.py.credit.common.logs.ExceptionLogger;
import com.py.credit.common.rest.RestServiceResult;
import com.py.credit.entity.SysUser;
import com.py.credit.entity.common.OptionalColumn;
import com.py.credit.entity.enums.RetrospectiveTableFieldEnum;
import com.py.credit.service.LhjcRemoteBaseService;
import com.py.credit.service.RetrospectiveService;
import com.py.credit.utils.ExcelUtil;
import com.py.credit.utils.MapCache;
import com.py.framework.core.helper.HttpServletHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/retrospective")
public class RetrospectiveController {

    private final static String FRONT_TABLE_ZSSJ_ID = "zssj";

    @Autowired
    private RetrospectiveService retrospectiveService;

    @Autowired
    private LhjcRemoteBaseService lhjcRemoteBaseService;

    /**
     * 追溯审计普通检索
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getOptionalColums.json")
    public RestServiceResult getOptionalColums() {
        return new RestServiceResult(RetrospectiveTableFieldEnum.getOptionalColumns());
    }

    /**
     * 追溯审计高级检索
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getAdvancedConditionColumns.json")
    public RestServiceResult getAdvancedConditionColumns() {
        return new RestServiceResult(RetrospectiveTableFieldEnum.getAdvancedConditionColumns());
    }

    /**
     * 追溯审计分页列表
     *
     * @param condition 查询条件
     * @param page      分页条件
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/list.json")
    public RestServiceResult getList(@RequestParam(value = "condition", defaultValue = "", required = true) String condition,
                                     Page page,
                                     HttpServletRequest request) {
        RestServiceResult<SysUser> user = lhjcRemoteBaseService.getCurrentUser(HttpServletHelper.getUserToken(request));
        String[] configColumns = null;
        List<String> showColumns = null;

        if (RestServiceResult.CODE_SUCCESS == user.getCode()) {
            SysUser userInfo = user.getData();
            if (null != userInfo) {
                configColumns = (String[]) MapCache.get(userInfo.getId() + "_" + FRONT_TABLE_ZSSJ_ID);
                showColumns = (configColumns == null || configColumns.length == 0)
                        ? RetrospectiveTableFieldEnum.getDefaultShowColumnEN() : Arrays.asList(configColumns);
            } else {
                ExceptionLogger.error("RetrospectiveController#getList", "未获取到当前用户信息！");
            }
        } else {
            ExceptionLogger.error("RetrospectiveController#getList", "未获取到当前用户信息！");
        }

        String[] additionalColumns = {"id"};

        List<Map<String, Object>> tableList = retrospectiveService.getByConditionForPage(showColumns,
                additionalColumns, condition, page);

        return new RestServiceResult(page.getTotalCount(), tableList);
    }

    /**
     * 查看详情
     *
     * @param id 主键id
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/detail.json")
    public RestServiceResult getDetail(@RequestParam(value = "id", defaultValue = "", required = true) String id) {
        return new RestServiceResult(retrospectiveService.getDetail(id));
    }

    /**
     * 自定义列表选项
     *
     * @param columns 列表数组
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/addShowColunms.json")
    public RestServiceResult<String> addShowColunms(@RequestParam(value = "columns", required = true) String[] columns,
                                                    HttpServletRequest request) {

        RestServiceResult<SysUser> user = lhjcRemoteBaseService.getCurrentUser(HttpServletHelper.getUserToken(request));

        if (RestServiceResult.CODE_SUCCESS == user.getCode()) {

            SysUser userInfo = user.getData();

            if (null != userInfo) {

                MapCache.set(userInfo.getId() + "_" + FRONT_TABLE_ZSSJ_ID, columns);

            } else {

                ExceptionLogger.error("RetrospectiveController#addShowColunms", "未获取到当前用户信息！");
            }

        } else {

            ExceptionLogger.error("RetrospectiveController#addShowColunms", "未获取到当前用户信息！");
        }

        return new RestServiceResult<String>();
    }

    /**
     * 高级检索文件导出
     *
     * @param condition 查询条件
     * @param columnEN  字段名
     * @param page      分页条件
     * @param response
     * @throws IOException
     */
    @ResponseBody
    @RequestMapping(value = "/export.json")
    public void export(@RequestParam(value = "condition", defaultValue = "", required = true) String condition,
                       @RequestParam(value = "columnEN", defaultValue = "", required = true) String columnEN, Page page,
                       HttpServletResponse response) throws IOException {

        page.setLimit(1000);

        List<String> columns = JSONArray.parseArray(columnEN, String.class);

        List<Map<String, Object>> data = retrospectiveService.getByConditionForPage(columns,
                null, condition, page);

        List<OptionalColumn> optionalColumns = null;

        optionalColumns = RetrospectiveTableFieldEnum.getOptionalColumns();

        ExcelUtil.downloadExcel(response, data, "追溯审计", "追溯审计", columns, optionalColumns);
    }
}
